/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "utils.h"

#include <QObject>
#include <QWidget>
#include <QIcon>
#include <QMenu>
#include <QAction>
#include <QString>
#include <QXmlStreamReader>

#include <QFile>

#include <QDebug>

#include "globals.h"

QAction* Utils :: addActionInMenu(QWidget* pParent,
                         QMenu* pMenu,
                         const QIcon& icon,
                         const QString& entryName,
                         const QString& keySequence,
                         const QString& tipMessage,
                         const QObject* object,
                         const char* slotName,
                         const bool isEnabled /* = true */,
                         const bool isCheckable /* = false */)
{
    QAction* action = new QAction(pParent);
    action = pMenu->addAction(entryName);
    action->setIcon(icon);
    action->setShortcut(QKeySequence(keySequence));
    action->setStatusTip(tipMessage);
    action->setEnabled(isEnabled);
    action->setCheckable(isCheckable);
    action->setChecked(isCheckable);

    pParent->connect(action, SIGNAL(triggered()), object, slotName);

    return action;
}

const QString Utils :: getFlagPathFromName(const QString& flagName)
{
    QString flagPath = FLAGS_DIR;

    qDebug() << "Utils :: getFlagFromName (" << flagName << ")";

    if ( flagName != "" && !flagName.isNull() )
    {
        flagPath = flagPath + flagName + QString(".png");
        if ( !QFile::exists(flagPath) )
        {
            // Default necta flag
            flagPath = FLAGS_DIR + QString("nectaflag.png");
        }
    }
    else
    {
        // Default necta flag
        flagPath = flagPath + QString("nectaflag.png");
    }

    return flagPath;
}

bool Utils :: getXMLStringData(QXmlStreamReader& xmlReader, QString& data)
{
    QXmlStreamReader::TokenType nodeType = xmlReader.readNext();

    if ( nodeType == QXmlStreamReader::Characters )
    {
        data = xmlReader.text().toString();
    }
    else if ( nodeType == QXmlStreamReader::EndElement ) // This case happens when empty string (e.g.: <tag/> )
    {
        data = "";
        return true;
    }
    else
    {
        qDebug() << "Utils :: getXMLStringData -> Was expecting some data ... but the nodeType is: " << nodeType << " in: " << xmlReader.name();
        return false;
    }

    return true;
}

bool Utils :: booleanFactory(const QString& booleanString, bool* isOk/* = 0*/)
{
    QString lBooleanString = booleanString.toLower();

    // By default, we will be able to convert the string
    if ( isOk != NULL )
    {
        *isOk = true;
    }

    if ( lBooleanString == "true" )
    {
        return true;
    }
    else if ( lBooleanString == "false" )
    {
        return false;
    }
    else
    {
        qDebug() << "Utils :: booleanFactory -> Failed to parse the string (" << booleanString << ")";

        if ( isOk != NULL )
        {
            *isOk = false;
        }
    }

    return false; // Default value
}

unsigned int Utils :: unlocalizedMonthFromString(const QString& stringDate)
{
    if ( stringDate.contains("January") )
    {
        return 1;
    }
    else if ( stringDate.contains("February") )
    {
        return 2;
    }
    else if ( stringDate.contains("March") )
    {
        return 3;
    }
    else if ( stringDate.contains("April") )
    {
        return 4;
    }
    else if ( stringDate.contains("May") )
    {
        return 5;
    }
    else if ( stringDate.contains("June") )
    {
        return 6;
    }
    else if ( stringDate.contains("July") )
    {
        return 7;
    }
    else if ( stringDate.contains("August") )
    {
        return 8;
    }
    else if ( stringDate.contains("September") )
    {
        return 9;
    }
    else if ( stringDate.contains("October") )
    {
        return 10;
    }
    else if ( stringDate.contains("November") )
    {
        return 11;
    }
    else if ( stringDate.contains("December") )
    {
        return 12;
    }

    return 0;
}
